<?php
/**
 * User: wangdong
 * Date: 2020-07-06
 * Time: 20:05
 */

namespace app\lib\exception;

use think\Log;
use think\Request;
use Exception;
use think\exception\Handle;

class ExceptionHandler extends Handle
{
    private $code;
    private $msg;
    private $errorCode;
    // 还需要返回客户端当前请求的URL地址
    public function render(Exception $e){
        if($e instanceof BaseException){
            $this->code = $e->code;
            $this->msg = $e->msg;
            $this->errorCode = $e->errorCode;
        }else {
            if(config('app_debug')){
                return parent::render($e);
            }else{
                $this->code = 500;
                $this->msg = '服务器内部异常';
                $this->errorCode = 999;
                $this->recordErrorlog($e);
            }

        }
        $request = Request::instance();
        $result=[
            'msg' => $this->msg,
            'error_code' => $this->errorCode,
            'request_url' => $request->url()
        ];
        return json($result,$this->code);
    }
    private function recordErrorlog(Exception $e){
        Log::init([
            'type' => 'File',
            'path' => LOG_PATH,
            'level' => ['error']
        ]);
        Log::record($e->getMessage(),'error');
    }

}